const axios = require('axios')
const query = require('./query')
const fs = require('fs')
const moment = require('moment')
const uuid = require('uuid')

axios.interceptors.response.use(res => {
  return res.data
})

const api = 'http://ts.api.chenggua.com/taoshu/lib/search'
const limit = 1000


/*
 *@Description: 采集小说信息
 *@params: 
 *@Author:  lupan
 *@Date: 2019-07-09 22:24:12
*/
const getHttpBookData = (index) => {
  return new Promise(reslove => {
    axios.post(api, {
      primaryType: 0,
      status: 0,
      words: 0,
      limit: limit,
      offset: index * limit
    }).then(async(res) => {
      const data = res.result
      await writeSql(data, index)
      if (data.length === limit) {
        index ++
        getHttpBookData(index)
      } else {
        console.log('全部录入成功' + moment().format('YYYY-MM-DD hh:mm:ss'))
      }
    })
  })
}



/*
 *@Description: 写库操作
 *@params: 
 *@Author:  lupan
 *@Date: 2019-07-09 22:24:23
*/
const writeSql = (data, index) => {
  return new Promise(async(reslove) => {
    const DATA = await filterExistBook(data)
    let sql = `INSERT into book_info (t_id, name, cover, author, words, star, intro, type, primaryType, status) values`
    DATA.forEach(item => {
      sql += `('${item.id}', '${item.name.replace(/'/g, '"')}', '${item.img}', '${item.author.replace(/'/g, '"')}', ${item.words}, ${item.star}, '${item.intro.replace(/'/g, '"')}', '${item.type}', ${item.primaryType}, ${item.status}),`
    })
    sql = sql.substring(0, sql.length - 1)
    query(sql).then(async(dbData) => {
      if (dbData.code) {
        console.log(`第${index}页写入成功 ${moment().format('YYYY-MM-DD hh:mm:ss')}`)
      } else {
        console.log(`第${index}页写入失败，记录错误日志 ${moment().format('YYYY-MM-DD hh:mm:ss')}`)
        await errorLog(data)
      }
      reslove()
    })
  })
}

/*
 *@name: 数组取差集
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-12 09:40:06
*/
const arr_dive = (arr1, arr2) => { //第一个数组减去第二个数组
  arr1 = arr1.map(item => {
    return item.replace(/\'/g, '')
  })
  var result = arr1.concat(arr2 ).filter(function (v) {
    return arr1.indexOf(v) === -1 || arr2 .indexOf(v) === -1
  })
  return result;
}

/*
 *@name: 根据ids从数组取出对应数据
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-12 09:53:53
*/
const getOutData = (ids, data) => {
  // console.log(data, ids)
  let arr = []
  data.forEach(di => {
    ids.forEach(ii => {
      if (di.id === ii) {
        arr.push(di)
      }
    })
  })
  return arr
}


/*
 *@name: 过滤已存在的书籍
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-11 09:40:09
*/
const filterExistBook = (data) => {
  return new Promise(reslove => {
    // 取出所有book  id
    const IDS = data.map(item => `'${item.id}'`)
    const sql = `select t_id from book_info where t_id in (${IDS.join(',')})`
    query(sql).then(async(dbData) => {
      if (dbData.code) {
        if (!dbData.data.length) {
          reslove(data)
        } else {
          const dbIDS = dbData.data.map(dbItem => {
            return dbItem.t_id
          })
          const ids = await arr_dive(IDS, dbIDS)
          const newData = await getOutData(ids, data)
          reslove(newData)
        }
      }
    })
  })
}

/*
 *@name: 根据日期生成日志文件名
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-12 10:03:49
*/
const getData = () => {
  const Y = moment().format('YYYY')
  const M = moment().format('MM')
  const D = moment().format('DD')
  return `${Y}_${M}_${D}.json`
}

/*
 *@name: 写错误日志
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-12 10:03:21
*/
const errorLog = (data) => {
  return new Promise(async(reslove) => {
    const path = `./log/${getData()}`
    let jsonContent = []
    for (let i = 0; i < data.length; i++) {
      const item = data[i]
      let content = `INSERT into book_info (t_id, name, cover, author, words, star, intro, type, primaryType, status) values ('${item.id}', '${item.name.replace(/'/g, '"')}', '${item.img}', '${item.author.replace(/'/g, '"')}', ${item.words}, ${item.star}, '${item.intro.replace(/'/g, '"')}', '${item.type}', ${item.primaryType}, ${item.status})\n`
      jsonContent.push({
        error_id: uuid.v4(),
        sql_content: content
      })
    }
    // 判断文件是否存在
    await fs.exists(path, exists => {
      if (exists) {
        // 存在取出已有日志，合并写入
        fs.readFile(path, (err, data) => {
          if (err) return console.error('日志文件读取失败：' + err)
          let person = data.toString() //将二进制的数据转换为字符串
          person = JSON.parse(person)
          jsonContent = jsonContent.concat(person)
          jsonContent = JSON.stringify(jsonContent)
          fs.writeFile(path, jsonContent, err => {
            if (err) return console.error('日志写入失败：' + err)
          })
        })
      } else {
        // 不存在直接写入
        jsonContent = JSON.stringify(jsonContent)
        fs.writeFile(path, jsonContent, err => {
          if (err) return console.error('日志写入失败：' + err)
        })
      }
    })
    await reslove()
  })
}


getHttpBookData(0)

